package org.toolbook.controller;

import java.util.Date;

import org.toolbook.interceptor.LoginInterceptor;
import org.toolbook.model.Diary;
import org.toolbook.model.User;
import org.toolbook.util.FileUtils;
import org.toolbook.util.LogUtil;
import org.toolbook.util.PropUtil;
import org.toolbook.validator.AddDiaryValidator;
import org.toolbook.validator.UpdateDiaryValidator;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
/**
 * 日记控制器
 * @author meigang
 * @date 2015-07-02 14:14
 *
 */
@Before(LoginInterceptor.class)
public class DiaryController extends Controller{
	private static String tablePre = PropUtil.getProperty("tablePre");
	/**
	 * 日记首页
	 */
	public void index(){
		//查询diary分页
		Integer pageNumber = getParaToInt("page");
		if(pageNumber == null){
			pageNumber = 1;
		}
		
		Page<Diary> diaryPage = Diary.dao.paginate(pageNumber, 
				Integer.parseInt(PropUtil.getProperty("DIARY_LIST_PAGE_SIZE")),
				"select *", "from "+tablePre+"diary order by isTop desc,publishTime desc");
		setAttr("diaryPage", diaryPage);
		render("/WEB-INF/pages/fc/diary/list.jsp");
	}
	/**
	 * 跳转到添加日记界面
	 */
	public void toAdd(){
		LogUtil.log.info("跳转到add diary界面");
		render("/WEB-INF/pages/fc/diary/add.jsp");
	}
	/**
	 * 添加日记
	 */
	@Before(AddDiaryValidator.class)
	public void add(){
		User u = getSessionAttr(PropUtil.getProperty("SESSION_LOGIN_KEY"));
		Diary diary = new Diary().set("title", getPara("title"))
				.set("lookPhoto", getPara("lookPhotoFilePath"))
				.set("note", getPara("note"))
				.set("content", getPara("content"))
				.set("publishTime", new Date())
				.set("updateTime", new Date())
				.set("readCount", 0)
				.set("weather", getPara("weather"))
				.set("isTop", 0)
				.set("userId", u.get("id"));
		diary.save();
		LogUtil.log.info("用户[ "+ u.get("username")+" ]新增日记成功");
		index();
	}
	/**
	 * 显示日记页
	 */
	public void show(){
		int id = getParaToInt(0);
		Diary diary = Diary.dao.findById(id);
		//更新阅读次数
		diary.set("readCount", diary.getInt("readCount")+1).update();
		setAttr("diary", diary);
		LogUtil.log.info("查看diary[id:"+id+"]");
		render("/WEB-INF/pages/fc/diary/show.jsp");
	}
	/**
	 * 删除
	 */
	public void delete(){
		int id = getParaToInt(0);
		Diary diary = Diary.dao.findById(id);
		FileUtils.deleteFile(diary.getStr("lookPhoto"));
		diary.delete();
		LogUtil.log.info("删除diary[id:"+id+"]");
		index();
	}
	/**
	 * 修改
	 */
	public void toUpdate(){
		int id = getParaToInt(0);
		Diary diary = Diary.dao.findById(id);
		setAttr("diary", diary);
		LogUtil.log.info("去修改界面diary[id:"+id+"]");
		render("/WEB-INF/pages/fc/diary/update.jsp");
	}
	/**
	 * 修改日记
	 */
	@Before(UpdateDiaryValidator.class)
	public void update(){
		User u = getSessionAttr(PropUtil.getProperty("SESSION_LOGIN_KEY"));
		Diary diary = Diary.dao.findById(getParaToInt("id"));
		diary.set("title", getPara("title"))
				.set("lookPhoto", getPara("lookPhotoFilePath"))
				.set("note", getPara("note"))
				.set("content", getPara("content"))
				.set("updateTime", new Date())
				.set("weather", getPara("weather"));
		diary.update();
		LogUtil.log.info("用户[ "+ u.get("username")+" ]修改diary[id:"+diary.getInt("id")+"]日记成功");
		setAttr("diary", diary);
		render("/WEB-INF/pages/fc/diary/show.jsp");
	}
	/**
	 * 置顶
	 */
	public void top(){
		Diary diary = Diary.dao.findById(getParaToInt(0)).set("isTop", 1);
		diary.update();
		LogUtil.log.info("置顶diary[id:"+getParaToInt(0)+"]日记成功");
		setAttr("diary", diary);
		render("/WEB-INF/pages/fc/diary/show.jsp");
	}
	/**
	 * 取消置顶
	 */
	public void notop(){
		Diary diary = Diary.dao.findById(getParaToInt(0)).set("isTop", 0);
		diary.update();
		LogUtil.log.info("取消置顶diary[id:"+getParaToInt(0)+"]日记成功");
		setAttr("diary", diary);
		render("/WEB-INF/pages/fc/diary/show.jsp");
	}
	
	
}
